/**
 * 
 */
package com.jamcodingnm.apps.abqevents.widget;

import java.util.List;

import com.jamcodingnm.apps.abqevents.R;
import com.jamcodingnm.apps.abqevents.R.id;
import com.jamcodingnm.apps.abqevents.R.layout;
import com.jamcodingnm.apps.abqevents.rss.RSSItem;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * @author jeremy
 *
 */
public class RSSAdapter extends BaseAdapter {
	private Activity mActivity;
	private List<RSSItem> mData;
	private static LayoutInflater inflater;
	
	public RSSAdapter(Activity activity, List<RSSItem> data) {
		this.mActivity = activity;
		this.mData = data;
		inflater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getCount()
	 */
	public int getCount() {
		return mData.size();
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItem(int)
	 */
	public Object getItem(int position) {
		return position;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItemId(int)
	 */
	public long getItemId(int position) {
		return position;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		if (convertView == null) {
			view = inflater.inflate(R.layout.list_item, null);
		}
		
		TextView title = (TextView) view.findViewById(R.id.item_title);
		TextView description = (TextView) view.findViewById(R.id.item_description);
		
		RSSItem item = mData.get(position);
		
		title.setText(item.getTitle());
		String text = item.getDescription();
		if ( text != null && text.trim().length() > 0) {
			description.setText(item.getDescription());
		} else {
			description.setText("No Description");
		}
		
		return view;
	}

}

